Page({
  data: {
    post: {},
    commentInputs: [], // 评论输入框的内容
    index:null,
    content:'',
    posts:[]
  },
  onLoad(options) {
    const index = options.index; // 假设传递了动态的唯一标识id
    // 根据postId从存储或其他数据源获取动态详情
    const posts = wx.getStorageSync('posts') || [];
    this.setData({
      index:index,
      posts:posts,
      commentInputs:posts[index].commentInputs
    });
    const post = posts[index]
    this.setData({ post:post });
  },
  onCommentInput(e) {
    const content = e.detail.value.trim();
    this.setData({
      content:content
    });
  },
  addComment() {
    const content = this.data.content;
    if (!content) {
      wx.showToast({
        title: '评论不能为空',
        icon: 'none'
      });
      return;
    }

    const currentUser = {
      commentAvatar: wx.getStorageSync('userInfo').avatarUrl,
      commentName: wx.getStorageSync('userInfo').nickName,
      content: content
    };
    // 添加评论
    this.data.commentInputs.unshift(currentUser)
    const commentInputs=this.data.commentInputs

    // 将评论数组添加到post对象
    this.data.post.commentInputs=commentInputs
    const index=this.data.index
    // 将post发帖添加到posts发帖合集
    this.data.posts[index]=this.data.post
    const posts=this.data.posts
    wx.setStorageSync('posts', posts);
    // 清空输入框
    this.setData({content:''})
  }
})